package library.and.xps.com.step.util;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Random;

import io.realm.Realm;
import io.realm.RealmResults;
import library.and.xps.com.step.m.model.DaliyRecord;
import library.and.xps.com.step.m.model.WeeklyRecord;

//用来模拟步数记录
public class StepGenerator {

    Realm realm;

    private static final StepGenerator ourInstance = new StepGenerator();

    public static StepGenerator getInstance() {
        return ourInstance;
    }

    private StepGenerator() {
        realm = Realm.getDefaultInstance();
    }

    //生成随机数据
    public void insertRecord(int days) {
        ArrayList<DaliyRecord> daliyRecords = new ArrayList<>();

        Random r = new Random();
        for (int i = 0; i < days; i++) {
            DaliyRecord daliyRecord = new DaliyRecord();
            Date d = getAddDayDate(i);
            daliyRecord.setDate(d);
            daliyRecord.setTimeStamp(DateUtil.getDateMilliseconds(d));
            daliyRecord.setStepCount(r.nextInt(4000));
            daliyRecords.add(daliyRecord);
        }

        realm.beginTransaction();
        realm.insert(daliyRecords);
        realm.commitTransaction();
    }

    private Date getAddDayDate(int addDay) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, addDay);
        return calendar.getTime();
    }

    public RealmResults<DaliyRecord> getRecordAll() {
        return realm.where(DaliyRecord.class).findAll();
    }

    /**
     * 获取周视图的数据
     * 应该在记录数量大于7的时候才能调用
     */
    public ArrayList<WeeklyRecord> getWeeklyRecord() {
        int i;
        int index = 0;
        ArrayList<WeeklyRecord> weekRecords = new ArrayList<>();
        ArrayList<DaliyRecord> allRecords = RToA(realm.where(DaliyRecord.class).findAll());
        int firstDayOfweek = DateUtil.getDayofWeek(allRecords.get(0).getTimeStamp());

        i = 8 - firstDayOfweek;

        if (i != 0) {
            WeeklyRecord weeklyRecord = new WeeklyRecord();
            ArrayList<DaliyRecord> eada = new ArrayList<>(allRecords.subList(0, i));
            weeklyRecord.setWeek(eada);
            weekRecords.add(weeklyRecord);
            index = i;
        }else i=7;

        for (;;i += 7) {
            if (i < allRecords.size()) {
                WeeklyRecord week = new WeeklyRecord();
                week.setWeek(new ArrayList<>(allRecords.subList(index, i)));
                weekRecords.add(week);
            }else {//边界情况 到这里i可能会大于allRecords的size
                WeeklyRecord week = new WeeklyRecord();
                week.setWeek(new ArrayList<>(allRecords.subList(index, allRecords.size())));
                weekRecords.add(week);
                break;
            }
            index = i;
        }

        Iterator<WeeklyRecord> iterator = weekRecords.iterator();
        while (iterator.hasNext()) {
            WeeklyRecord week = iterator.next();
            if (week.getWeek().size() <= 0)
                iterator.remove();
        }

        return weekRecords;
    }

    public boolean deleteAll() {
        boolean success;
        realm.beginTransaction();
        success = realm.where(DaliyRecord.class).findAll().deleteAllFromRealm();
        realm.commitTransaction();
        return success;
    }

    public ArrayList<DaliyRecord> DayRecordSupplement(RealmResults<DaliyRecord> records) {
        int total = 7;

        ArrayList<DaliyRecord> records1 = RToA(records);

        while (records1.size() < total) {
            DaliyRecord daliyRecord = new DaliyRecord();
            Date d = getAddDayDate(total);
            daliyRecord.setDate(d);
            daliyRecord.setTimeStamp(DateUtil.getDateMilliseconds(d));
            daliyRecord.setStepCount(0);
            records1.add(daliyRecord);
        }

        return records1;
    }

    public ArrayList<WeeklyRecord> WeekRecordSupplement(ArrayList<WeeklyRecord> records) {
        ArrayList<WeeklyRecord> weeklyRecords = records;
        while (!(weeklyRecords.size()>=7)){
            WeeklyRecord weeklyRecord = new WeeklyRecord();
            weeklyRecords.add(weeklyRecord);
        }

        return weeklyRecords;
    }

    /**
     * RealmResult转换成Arraylist
     */
    public ArrayList<DaliyRecord> RToA(RealmResults<DaliyRecord> rrData) {
        ArrayList<DaliyRecord> re = new ArrayList<>();
        for (int i = 0; i < rrData.size(); i++) {
            re.add(rrData.get(i));
        }
        return re;
    }

}
